package com.example.moblie2030606155byl0328;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.text.BreakIterator;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView;
    private EditText editTextTextPersonName;
    private Button btnQingchu;
    private Button btnCheng;
    private Button btnChu;
    private Button btnTuige;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private Button btnJian;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btnBaifeibi;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btnJia;
    private Button btn0;
    private Button btnDian;
    private Button btnDengyu;
    double num1=0,num2=0;
    double result=0;
    int   op=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);

        btnJia.setOnClickListener(this);
        btnJian.setOnClickListener(this);
        btnCheng.setOnClickListener(this);
        btnChu.setOnClickListener(this);
        btnQingchu.setOnClickListener(this);
        btnDian.setOnClickListener(this);
        btnDengyu.setOnClickListener(this);
    }

    private void initView() {
        textView = findViewById(R.id.textView);
        editTextTextPersonName = findViewById(R.id.editTextTextPersonName);
        btnQingchu = findViewById(R.id.btn_qingchu);
        btnCheng = findViewById(R.id.btn_cheng);
        btnChu = findViewById(R.id.btn_chu);
        btnTuige = findViewById(R.id.btn_tuige);
        btn7 = findViewById(R.id.btn_7);
        btn8 = findViewById(R.id.btn_8);
        btn9 = findViewById(R.id.btn_9);
        btnJian = findViewById(R.id.btn_jian);
        btn4 = findViewById(R.id.btn_4);
        btn5 = findViewById(R.id.btn_5);
        btn6 = findViewById(R.id.btn_6);
        btnBaifeibi = findViewById(R.id.btn_baifeibi);
        btn1 = findViewById(R.id.btn_1);
        btn2 = findViewById(R.id.btn_2);
        btn3 = findViewById(R.id.btn_3);
        btnJia = findViewById(R.id.btn_jia);
        btn0 = findViewById(R.id.btn_0);
        btnDian = findViewById(R.id.btn_dian);
        btnDengyu = findViewById(R.id.btn_dengyu);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btn_qingchu:
                editTextTextPersonName.setText(null);
                break;

            case R.id.btn_1:
                String str1=editTextTextPersonName.getText().toString();
                str1+="1";
                editTextTextPersonName.setText(str1);
                break;
            case R.id.btn_2:
                String str2=editTextTextPersonName.getText().toString();
                str2+="2";
                editTextTextPersonName.setText(str2);
                break;
            case R.id.btn_3:
                String str3=editTextTextPersonName.getText().toString();
                str3+="3";
                editTextTextPersonName.setText(str3);
                break;
            case R.id.btn_4:
                String str4=editTextTextPersonName.getText().toString();
                str4+="4";
                editTextTextPersonName.setText(str4);
                break;
            case R.id.btn_5:
                String str5=editTextTextPersonName.getText().toString();
                str5+="5";
                editTextTextPersonName.setText(str5);
                break;
            case R.id.btn_6:
                String str6=editTextTextPersonName.getText().toString();
                str6+="6";
                editTextTextPersonName.setText(str6);
                break;
            case R.id.btn_7:
                String str7=editTextTextPersonName.getText().toString();
                str7+="7";
                editTextTextPersonName.setText(str7);
                break;
            case R.id.btn_8:
                String str8=editTextTextPersonName.getText().toString();
                str8+="8";
                editTextTextPersonName.setText(str8);
                break;
            case R.id.btn_9:
                String str9=editTextTextPersonName.getText().toString();
                str9+="9";
                editTextTextPersonName.setText(str9);
                break;
            case R.id.btn_dian:
                String strd=editTextTextPersonName.getText().toString();
                strd+=".";
                editTextTextPersonName.setText(strd);
                break;
            case R.id.btn_0:
                String str0=editTextTextPersonName.getText().toString();
                str0+="0";
                editTextTextPersonName.setText(str0);
                break;

            //运算按钮
            case R.id.btn_jia:
                String stradd=editTextTextPersonName.getText().toString();
                if(stradd.equals(null))
                {
                    return;
                }
                num1=Double.valueOf(stradd);
                editTextTextPersonName.setText(null);
                op=1;
                break;
            case R.id.btn_jian:
                String strjian=editTextTextPersonName.getText().toString();
                if(strjian.equals(null))
                {
                    return;
                }
                num1=Double.valueOf(strjian);
                editTextTextPersonName.setText(null);
                op=2;
                break;
            case R.id.btn_cheng:
                String strcheng=editTextTextPersonName.getText().toString();
                if(strcheng.equals(null))
                {
                    return;
                }
                num1=Double.valueOf(strcheng);
                editTextTextPersonName.setText(null);
                op=3;
                break;
            case R.id.btn_chu:
                String strchu=editTextTextPersonName.getText().toString();
                if(strchu.equals(null))
                {
                    return;
                }
                num1=Double.valueOf(strchu);
                editTextTextPersonName.setText(null);
                op=4;
                break;


            case R.id.btn_dengyu:
                String strdengyu=editTextTextPersonName.getText().toString();
                num2=Double.valueOf(strdengyu);
                editTextTextPersonName.setText(null);
                switch (op) {
                    case 1:
                        result = num1 + num2;
                        editTextTextPersonName.setText(String.valueOf(num1) + "+" + String.valueOf(num2) + "=" + String.valueOf(result));
                        break;
                    case 2:
                        result = num1 - num2;
                        editTextTextPersonName.setText(String.valueOf(num1) + "-" + String.valueOf(num2) + "=" + String.valueOf(result));
                        break;
                    case 3:
                        result = num1 * num2;
                        editTextTextPersonName.setText(String.valueOf(num1) + "*" + String.valueOf(num2) + "=" + String.valueOf(result));
                        break;
                    case 4:
                        if (num2 == 0) {
                            editTextTextPersonName.setText("除数不能为0！");
                        } else {
                            result = num1 / num2;
                            editTextTextPersonName.setText(String.valueOf(num1) + "/" + String.valueOf(num2) + "=" + String.valueOf(result));
                        }
                        break;
                    default:
                        result = 0;
                        break;

                }



        }
    }



}